import {$} from '../js/library/jquery.js';
import './library/jquery.lazyload.js';
import {cookie} from '../js/library/cookie.js';
import {querystring} from '../js/library/qs.js';

// 关闭广告
$('.close').on('click',function(){
  $('.banner').css('display','none');
})

// 页面渲染
$.ajax({
  type: "get",
  url: "../../interface/getData.php",
  dataType: "json",
  success: function (res) {

    let temp = '';

    res.forEach((elm,i) => {
      let picture = JSON.parse(elm.picture);

      temp += `<div class="con9-list">
      <a href="./product.html?id=${elm.id}">
      <img data-original="${picture[0].src}" alt="" class="lazy"/>
      <p class="con9-p1">${elm.title}</p>
      <p class="con9-p2">${elm.details}</p>
      <p class="con9-p3">￥${elm.price}</p>
      </a>
    </div>`;
    });
    $('.con9').append(temp);
    
    // 懒加载
    $(function(){
      $("img.lazy").lazyload({
        placeholder: "../images/jiazai.gif",
        effect: "fadeIn"
      });
    })
  } 
});

$('a').on('hover',function(){
  $(this).css('text-decoration', 'none');
})

// 固定导航栏
$(window).on('scroll',function(){
  let scrollTop = $('html').scrollTop();
  let nap_top = $('.con1').offset().top;
  if(nap_top-scrollTop <= 0){
    $('.fix>.nav:last').css('display','block'); 
  }else{
  $('.fix>.nav:last').css('display','none'); 
  }
})
// 返回顶部
$('.fix>.nav:last').on('click',function(){
  $('html').animate({
    scrollTop: 0
  },300)
 })

// 文字轮播
let liHight = $('.tig-top>.top-list>li').height();
let i = 0;
let timer1 = null;
timer1 = setInterval(()=>{
  let top = `-=${liHight}`
$('.loding-rig>.tig-top>.top-list').animate({
  top:top
},400,function(){
  i++; 
  if(i == 5){
    i = 0;
    $('.loding-rig>.tig-top>.top-list').css('top',0)
  }
})
},2000)
// 鼠标移入停止
$('.top-list').on('mouseenter',function(){
  clearInterval(timer1);
});
// 鼠标移出继续
$('.top-list').on('mouseleave',function(){
  timer1 = setInterval(()=>{
    let top = `-=${liHight}`
    $('.loding-rig>.tig-top>.top-list').animate({
      top:top
    },400,function(){
      i++; 
      if(i == 5){
        i = 0;
        $('.loding-rig>.tig-top>.top-list').css('top',0)
      }
    })
  },2000)
})

// 产品切换
//右
$('.swiper-button-next').on('click',function(){
  let left=parseInt($('.con5-main').css('left'));
  $('.swiper-button-prev').css('display','block');
  left -=1210;
  if(left<=-3630){
    $('.swiper-button-next').off('click')
  }
  $('.con5-main').animate({
    'left':left
  },600)

  // 左
  $('.swiper-button-prev').on('click',function(){
    let l = parseInt($('.con5-main').css('left'));
    l +=1210;
    if(l >= 0){
      $('.swiper-button-prev').off('click');
      $('.swiper-button-prev').css('display','none');
    }
    $('.con5-main').animate({
      'left':l
    },500)
  })
})

// 图片轮换
let src = ['../images/ban1.jpg',"../images/ban2.jpg","../images/ban3.jpg"];
let timer2 = null;
let n = 0;

timer2 = setInterval(() => {
  n++;
	if(n >= src.length){
		n = 0;
	}
	$('.con-ban>img').attr('src',src[n]);
	$('.con-ban>.ban-list>li').eq(n).addClass("active").siblings().removeClass("active");
},2000)
// 鼠标移入停止
$('.con-ban').on('mouseenter',function(){
  clearInterval(timer2);
});
// 鼠标移入继续
$('.con-ban').on('mouseleave',function(){
  timer2 = setInterval(() => {
    n++;
    if(n >= src.length){
      n = 0;
    }
    $('.con-ban>img').attr('src',src[n]);
    $('.con-ban>.ban-list>li').eq(n).addClass("active").siblings().removeClass("active");
  },2000)
})
// 鼠标移到小圈圈跳转
$('.con-ban>.ban-list>li').on('mouseover',function(){
  let index = $('.con-ban>.ban-list>li').index(this);
  $('.con-ban>img').attr('src',src[index]);
	$('.con-ban>.ban-list>li').eq(index).addClass("active").siblings().removeClass("active");
})

// 底部导航栏
// 右边角
$('.double>.sec>span:last-child').on('click',function(){
  $(this).css('cursor','not-allowed').siblings().css('cursor','pointer');

  $('.double>.ul1').animate({'margin-left':'-153px'},'normal');
})
// 左边角
$('.double>.sec>span:first-child').on('click',function(){
  $(this).css('cursor','not-allowed').siblings().css('cursor','pointer');
  
  $('.double>.ul1').animate({'margin-left':'0'},'normal');
})